package org.csu.myPetStore.web.servlet.ajax;

import jdk.nashorn.internal.parser.JSONParser;
import org.csu.myPetStore.domain.Product;
import org.csu.myPetStore.service.SearchService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class SearchProductAutoFill extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Enter Search");

        SearchService searchService = new SearchService();

        String keyword = req.getParameter("searchName");
        System.out.println("keyword:" + keyword);
        List<String> searchList = searchService.searchProductList(keyword);

        String back = "";
        for(int i=0;i<searchList.size();i++){
            back = back.concat(searchList.get(i)+"|");
            System.out.println("every:" + searchList.get(i));
        }



        System.out.println("find:" + back);

        resp.setContentType("text/plain");

        PrintWriter out = resp.getWriter();

        out.print(back);
        System.out.println("OK");
        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
